﻿using HDSmartCommunity.Models;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;

namespace HDSmartCommunity.BUS
{
    public class LogBUS
    {
        public ResponseMessage DownLoad()
        {
            ResponseMessage message = new ResponseMessage();
            List<LogInfo> list = new List<LogInfo>();
            string logPath = AppDomain.CurrentDomain.BaseDirectory + "/wwwroot/Log";
            if (Directory.Exists(logPath))
            {
                DirectoryInfo folder_base = new DirectoryInfo(logPath);
                FileInfo[] files_base = folder_base.GetFiles("*.txt");  //获取.txt文件
                foreach (var file_txt in files_base)
                {
                    LogInfo logInfo = new LogInfo();
                    //获取文件创建时间
                    logInfo.FileName = file_txt.Name;
                    logInfo.FilePath = "/Log/" + file_txt.Name;
                    list.Add(logInfo);
                }
            }
            message.total = list.Count();
            message.data = list;
            return message;
        }

        public ResponseMessage PZDownLoad(string starttime, string endtime, string numberno)
        {
            DateTime dt_start = Convert.ToDateTime(starttime).Date;
            DateTime dt_end = Convert.ToDateTime(endtime).Date;
            ResponseMessage message = new ResponseMessage();
            List<LogInfo> list = new List<LogInfo>();
            string logPath = AppDomain.CurrentDomain.BaseDirectory + "wwwroot/Log/" + numberno;
            if (Directory.Exists(logPath))
            {
                DirectoryInfo folder_base = new DirectoryInfo(logPath);
                FileInfo[] files_base = folder_base.GetFiles("*.txt");  //获取.txt文件
                foreach (var file_txt in files_base)
                {
                    DateTime filetime = file_txt.CreationTime.Date;
                    if (filetime >= dt_start && filetime <= dt_end)
                    {
                        LogInfo logInfo = new LogInfo();
                        //获取文件创建时间
                        logInfo.FileName = file_txt.Name;
                        logInfo.FilePath = "/Log/" + numberno + "/" + file_txt.Name;
                        list.Add(logInfo);
                    }
                }
            }
            message.total = list.Count();
            message.data = list;
            return message;
        }

        public ResponseMessage NetworkDownLoad()
        {
            ResponseMessage message = new ResponseMessage();
            List<LogInfo> list = new List<LogInfo>();
            string logPath = AppDomain.CurrentDomain.BaseDirectory + "wwwroot/TestLog/Network";
            if (Directory.Exists(logPath))
            {
                DirectoryInfo folder_base = new DirectoryInfo(logPath);
                FileInfo[] files_base = folder_base.GetFiles("*.txt");  //获取.txt文件
                foreach (var file_txt in files_base)
                {
                    LogInfo logInfo = new LogInfo();
                    //获取文件创建时间
                    logInfo.FileName = file_txt.Name;
                    logInfo.FilePath = "/TestLog/Network/" + file_txt.Name;
                    list.Add(logInfo);
                }
            }
            message.total = list.Count();
            message.data = list;
            return message;
        }

        public ResponseMessage SerialDownLoad()
        {
            ResponseMessage message = new ResponseMessage();
            List<LogInfo> list = new List<LogInfo>();
            string logPath = AppDomain.CurrentDomain.BaseDirectory + "wwwroot/TestLog/Serial";
            if (Directory.Exists(logPath))
            {
                DirectoryInfo folder_base = new DirectoryInfo(logPath);
                FileInfo[] files_base = folder_base.GetFiles("*.txt");  //获取.txt文件
                foreach (var file_txt in files_base)
                {
                    LogInfo logInfo = new LogInfo();
                    //获取文件创建时间
                    logInfo.FileName = file_txt.Name;
                    logInfo.FilePath = "/TestLog/Serial/" + file_txt.Name;
                    list.Add(logInfo);
                }
            }
            message.total = list.Count();
            message.data = list;
            return message;
        }

    }
}
